package com.uusee.crawler.pageprocessor.baike.douban;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

import com.uusee.crawler.framework.Processor;
import com.uusee.crawler.model.CrawlStatusCodes;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.util.PageProcessorUtils;
import com.uusee.shipshape.bk.model.Baike;

public class DoubanSearchResultPageProcessor extends Processor {
	private static Log log = LogFactory.getLog(DoubanSearchResultPageProcessor.class);
	private Properties pageProcessRegex;
	
	
	public void setPageProcessRegex(Properties pageProcessRegex) {
		this.pageProcessRegex = pageProcessRegex;
	}

	public void innerProcess(CrawlURI crawlURI) {
		if (!canProcess(crawlURI)) {
			return;
		}
		List<CrawlURI> finalCrawlURIList = new ArrayList<CrawlURI>();
		
		try {
			Date crawlDate = new Date();
			String crawlSite = crawlURI.getSourceSite();
			String charset = crawlURI.getCharset();
			String crawlUrl = crawlURI.getCrawlUrl();
			String crawlResult = crawlURI.getCrawlResult();
			
			//过滤
			String modelListHtml = crawlResult;
			if (pageProcessRegex.containsKey("filters")) {
				String filters = pageProcessRegex.getProperty("filters");
				String[] filterArray = filters.split("::");
				for (String filter:filterArray) {
					modelListHtml = modelListHtml.replaceAll(filter, "");
				}
			}
			
			//取得结果
			String modelListReg = pageProcessRegex.getProperty("modelList");
			
			modelListHtml = PageProcessorUtils.getValue(modelListReg, modelListHtml);

			PatternCompiler compiler = new Perl5Compiler();
			String modelRegex = pageProcessRegex.getProperty("model");
			Pattern pattern = compiler.compile(modelRegex, Perl5Compiler.CASE_INSENSITIVE_MASK);
			PatternMatcher matcher = new Perl5Matcher();
			PatternMatcherInput input = new PatternMatcherInput(modelListHtml);
			while (matcher.contains(input, pattern)) {
				try {
					MatchResult m = matcher.getMatch();
					String modelHtml = m.group(1);
					
					Baike newBaike = new Baike();

					String name = PageProcessorUtils.getValue(pageProcessRegex.getProperty("name"), modelHtml);
					String oriLogoUrl = PageProcessorUtils.getValue(pageProcessRegex.getProperty("oriLogoUrl"), modelHtml);
					String oriId = PageProcessorUtils.getValue(pageProcessRegex.getProperty("oriId"), modelHtml);
					String oriHtmlUrl = "http://www.douban.com/subject/"+oriId+"/";
					
					//验证
					if (StringUtils.isEmpty(name) || StringUtils.isEmpty(oriId)) {
						continue;
					}
					
					newBaike.setSourceSite(crawlSite);
					newBaike.setOriId(oriId);
					newBaike.setOriHtmlUrl(oriHtmlUrl);
					newBaike.setOriLogoUrl(oriLogoUrl);
					newBaike.setName(name);
					
					newBaike.setCreateUser("uusee_crawler");
					newBaike.setCreateDate(crawlDate);
					newBaike.setUpdateUser("uusee_crawler");
					newBaike.setUpdateDate(crawlDate);
					
					CrawlURI finalCrawlURI = new CrawlURI();
					finalCrawlURI.setSourceSite(crawlSite);
					finalCrawlURI.setCharset(charset);
					finalCrawlURI.setCrawlStatus(CrawlStatusCodes.DEFAULT_STATUS);
					finalCrawlURI.setCrawlUrl(oriHtmlUrl);
					finalCrawlURI.setModel(newBaike);
					
					finalCrawlURIList.add(finalCrawlURI);
				} catch (Exception e) {
					e.printStackTrace();
					log.error(e);
				}
			}
			
			if (finalCrawlURIList.size()>0) {
				crawlURI.setModel(finalCrawlURIList);
				crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_SUCCESS);
			}else{
				crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.equals(e);
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_EXCEPTION);
		}
	}
	
	private boolean canProcess(CrawlURI crawlURI) {
		int crawlStatus = crawlURI.getCrawlStatus();
		if (crawlStatus == CrawlStatusCodes.FETCH_SUCCESS) {
			return true;
		} else if (crawlStatus == CrawlStatusCodes.FETCH_SC_NOT_OK) {
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
			return false;
		} else {
			return false;
		}
	}
}
